package com.d404.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.server.DownloadInfo;
import im.beans.Group;
import im.beans.User;
import im.notify.server.Notify;
import manager.MessageManager;
import manager.PersistentManager;

/**
 * 从群组删除用户
 * Servlet implementation class DeleteFromGroup
 */
public class DeleteFromGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteFromGroup() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
		Transaction tx = session.beginTransaction();
		try
		{
			Query q = session.createQuery("delete from Group_User gu where gu.group = ? and gu.user = ?");
			Group gTmep = new Group();
			gTmep.setId(Long.parseLong(upload.getGroupId()));
			q.setParameter(0, gTmep);
			User tUser = new User();
			tUser.setId(Long.parseLong(upload.getUserId()));
			q.setParameter(1, tUser);
			q.executeUpdate();
			
			tx.commit();
			
			// 通知被从群组移除
			Notify.BeRemovedFromGroup beRemovedFromGroup = new Notify.BeRemovedFromGroup();
			beRemovedFromGroup.setGroupId(upload.getGroupId());
			MessageManager.sendSimpleMessage(upload.getAccount(), JSON.toJSONString(beRemovedFromGroup));
			download.setStatus(Download.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(Download.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	 public static class Upload
	    {
	        String groupId;
	        String userId;
	        String account;
	        
	        public Upload() {
			}

			public Upload(String groupId, String userId,String account) {
	            this.groupId = groupId;
	            this.userId = userId;
	            this.account = account;
	        }

	        public String getGroupId() {
	            return groupId;
	        }
	        public void setGroupId(String groupId) {
	            this.groupId = groupId;
	        }
	        public String getUserId() {
	            return userId;
	        }
	        public void setUserId(String userId) {
	            this.userId = userId;
	        }

	        public String getAccount() {
	            return account;
	        }

	        public void setAccount(String account) {
	            this.account = account;
	        }
	    }
	
	public static class Download
	{
		public static final String SUCCESS = "1";
		public static final String FAILED = "2";
		
		String status;

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}
		
	}

}
